/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：GoodsTypeDaoImpl.java
 * History:
 *       May 11, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.xteam.ecsystem.dao.IGoodsTypeAttrDao;
import com.xteam.ecsystem.entities.BaseGoodsType;
import com.xteam.ecsystem.entities.BaseGroupAttribute;
import com.xteam.ecsystem.entities.BaseTypeAttribute;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 商品类型属性数据访问实现类
 * 
 * @author liuzhi
 *
 */
@Repository
public class GoodsTypeAttrDaoImpl extends DDLPrefix implements IGoodsTypeAttrDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	/**
	 * 
	 * 新增或者修改商品类型属性信息
	 * @author liuzhi
	 * @param obj
	 * @param id
	 */
	public void saveOrUpdateObject(Object obj, String id){
		try {
			if (id != null && !id.equals("") && id.length() > 0){
				this.baseDao.update(obj);
			}else {
				this.baseDao.save(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 条件查询和分页显示商品类型属性信息
	 * @author liuzhi
	 * @param index
	 * @param size
	 * @param btaGtype
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getGoodsTypeAttrList(int pageIndex, int pageSize, String btaGtype){
		JSONObject jo = new JSONObject();
		try {
			List list = new ArrayList();
			
			String hql = SQL_SELECT + " new " + BaseTypeAttribute.REF 
				+ " (ta.btaId,ta.btaGtype,ta.btaName,ta.btaAtype,ta.btaMode,ta.btaValue," 
				+ "ta.btaOrder,ta.btaState,t.bgtName) " + SQL_FROM 
				+ BaseTypeAttribute.REF + " as ta, " + BaseGoodsType.REF + " as t" 
				+ SQL_WHERE + " ta.btaGtype= t.bgtId "; 
			
			String countHQL = SQL_SELECT + " count(*) " + SQL_FROM + BaseTypeAttribute.REF 
				+" as ta, " + BaseGoodsType.REF + " as t " + SQL_WHERE + " ta.btaGtype= t.bgtId ";
			
			if (btaGtype != null && btaGtype.length() > 0
					&& !btaGtype.equals("undefined")
					&& !btaGtype.equals("null")) {
				if(!"-1".equals(btaGtype)){
					hql += SQL_AND + " ta.btaGtype=? ";
					countHQL += SQL_AND + " ta.btaGtype=? ";
					
					list.add(btaGtype);
				}
			}
			hql += " order by ta.btaOrder";
			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao.queryList(countHQL, hql, list
					.toArray(), pageIndex * pageSize, pageSize);
			
			jo = JSONObject.fromObject(p);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	/**
	 * 根据商品类型属性编号获得对应的商品类型属性信息
	 * @author liuzhi
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getGoodsTypeAttrById(String id){
		JSONObject jo = new JSONObject();
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + BaseTypeAttribute.REF + SQL_WHERE +" 1=1 " + SQL_AND + " btaId=?";
			
			list = this.baseDao.find(hql, new Object[]{id});
			
			jo = JSONObject.fromObject((BaseTypeAttribute)list.get(0));

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}
	
	/**
	 * 
	 * 根据商品类型属性编号修改商品类型属性状态
	 * 0：无效
	 * 1：有效
	 * @author liuzhi
	 * @param id
	 */
	@SuppressWarnings("unchecked")
	public void updateBtaStateById(String id){
		Class clazz = null;
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseTypeAttribute");
			
			BaseTypeAttribute typeAttr = (BaseTypeAttribute)this.baseDao.get(clazz, id);
			if(typeAttr.getBtaState() == 1){
				typeAttr.setBtaState(0);
			}else{
				typeAttr.setBtaState(1);
			}
			
			this.baseDao.saveOrUpdate(typeAttr);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询JSON类型的商品类型名称和类型编号
	 * @author liuzhi
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String findGoodsTypeJson(String type){
		JSONObject jo = new JSONObject();
		JSONArray array = new JSONArray();
		List list = new ArrayList();
		try {
			String hql = SQL_FROM + BaseGoodsType.REF + SQL_WHERE +" 1=1 " /*+ SQL_AND + " bgtState=1"*/;
			
			list = this.baseDao.find(hql);
			
			if (type != null && type.length() > 0
					&& !type.equals("undefined")
					&& !type.equals("null")) {
				if("query".equals(type)){
					jo.put("bgtName", "所有商品类型");
					jo.put("bgtId", "-1");
					array.add(jo);
				}
			}
			
			for (int i = 0; i < list.size(); i++) {
				BaseGoodsType goodsType = (BaseGoodsType)list.get(i);
				array.add(goodsType);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return array.toString();
	}
	
	@SuppressWarnings("unchecked")
	public void delGoodsTypeAttrByIds(String btaIds){
		Class clazz = null;
		
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.BaseTypeAttribute");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		List list = new ArrayList();
		String oId[] = btaIds.split(",");
		for (String id : oId) {
			Object obj = this.baseDao.get(clazz, id);
			
			if (obj != null) {
				list.add(obj);
			}
		}
		
		this.baseDao.deleteAll(list);
	}
	
	@SuppressWarnings("unchecked")
	public boolean getBgaByBta(String btaIds){
		boolean isUsed = false;
		List list = new ArrayList();
		String hql = "";
		
		try {
			String oId[] = btaIds.split(",");
			for (String id : oId) {
				hql = SQL_FROM + BaseGroupAttribute.REF + SQL_WHERE + "bgaTattri = ?";
				list = this.baseDao.find(hql, new Object[]{id});
				
				if (list != null && !list.isEmpty()) {
					isUsed = true;
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return isUsed;
	}

}
